using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Markup;
using Common;
using SExperiment;
using SSettings;

namespace MainView.Windows
{
	public partial class AutoIndexWindow : Window, IComponentConnector
	{
		public delegate void DelegateOnAutoIndexChangedEvent(object sender, EventArgs e);

		public enum ESelectMode
		{
			All,
			Selected,
			Project
		}

		public static readonly DependencyProperty SelectModeProperty = DependencyProperty.Register("SelectMode", typeof(ESelectMode), typeof(AutoIndexWindow), new UIPropertyMetadata(ESelectMode.All));

		public static readonly DependencyProperty SelectedProjectProperty = DependencyProperty.Register("SelectedProject", typeof(string), typeof(AutoIndexWindow), new UIPropertyMetadata(""));

		public static readonly DependencyProperty StartMarkProperty = DependencyProperty.Register("StartMark", typeof(string), typeof(AutoIndexWindow), new UIPropertyMetadata(""));

		public static readonly DependencyProperty StartIndexProperty = DependencyProperty.Register("StartIndex", typeof(int), typeof(AutoIndexWindow), new UIPropertyMetadata(1));

		public static readonly DependencyProperty IntervalProperty = DependencyProperty.Register("Interval", typeof(int), typeof(AutoIndexWindow), new UIPropertyMetadata(1));

		public static readonly DependencyProperty FormatLengthProperty = DependencyProperty.Register("FormatLength", typeof(int), typeof(AutoIndexWindow), new UIPropertyMetadata(1));

		public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register("StringFormat", typeof(string), typeof(AutoIndexWindow), new UIPropertyMetadata(""));

		private MemorySettings ms = ConfigSettings.GetInstance().MemorySettings;

		public ESelectMode SelectMode
		{
			get
			{
				return (ESelectMode)GetValue(SelectModeProperty);
			}
			set
			{
				SetValue(SelectModeProperty, value);
			}
		}

		public string SelectedProject
		{
			get
			{
				return (string)GetValue(SelectedProjectProperty);
			}
			set
			{
				SetValue(SelectedProjectProperty, value);
			}
		}

		public string StartMark
		{
			get
			{
				return (string)GetValue(StartMarkProperty);
			}
			set
			{
				SetValue(StartMarkProperty, value);
			}
		}

		public int StartIndex
		{
			get
			{
				return (int)GetValue(StartIndexProperty);
			}
			set
			{
				SetValue(StartIndexProperty, value);
			}
		}

		public int Interval
		{
			get
			{
				return (int)GetValue(IntervalProperty);
			}
			set
			{
				SetValue(IntervalProperty, value);
			}
		}

		public int FormatLength
		{
			get
			{
				return (int)GetValue(FormatLengthProperty);
			}
			set
			{
				SetValue(FormatLengthProperty, value);
			}
		}

		public string StringFormat
		{
			get
			{
				return (string)GetValue(StringFormatProperty);
			}
			set
			{
				SetValue(StringFormatProperty, value);
			}
		}

		public List<Sample> AllSamples { get; set; }

		public List<Sample> SelectSamples { get; set; }

		public bool IsModify { get; set; }

		public event DelegateOnAutoIndexChangedEvent OnAutoIndexChanged;

		public AutoIndexWindow()
		{
			InitializeComponent();
		}

		private void Root_Loaded(object sender, RoutedEventArgs e)
		{
			List<string> list = (from s in AllSamples
				where s.Project != null
				select s.Project.BasicInfo.Name).Distinct().ToList();
			
			int num = SelectSamples.Count();
			
			List<string> list2 = new List<string>();
			list2.Add(Utility.GetMessage("all_sample"));

			if (num > 0)
			{
				list2.Add(Utility.GetMessage("selected_sample"));
			}
			if (list != null)
			{
				list2.AddRange(list);
			}
			comboBoxRange.ItemsSource = list2;
			SelectedProject = list2[0];
			if (num > 1)
			{
				comboBoxRange.SelectedIndex = 1;
			}
			Interval = ms.AutoIndexInterval;
			FormatLength = ms.AutoIndexFormatLength;
			textBoxStartMark.Focus();
		}

		private void buttonOK_Click(object sender, RoutedEventArgs e)
		{
			ms.AutoIndexInterval = Interval;
			ms.AutoIndexFormatLength = FormatLength;
			switch (comboBoxRange.SelectedIndex)
			{
			case 0:
				SelectMode = ESelectMode.All;
				break;
			case 1:
				if (SelectSamples.Count > 0)
				{
					SelectMode = ESelectMode.Selected;
				}
				else
				{
					SelectMode = ESelectMode.Project;
				}
				break;
			default:
				SelectMode = ESelectMode.Project;
				break;
			}
			StringFormat = "";
			for (int i = 0; i < FormatLength; i++)
			{
				StringFormat += "0";
			}
			IList<Sample> list = AllSamples;
			switch (SelectMode)
			{
			case ESelectMode.Selected:
				list = SelectSamples;
				break;
			case ESelectMode.Project:
				list = list.Where((Sample s) => s.Project.BasicInfo.Name == SelectedProject).ToList();
				break;
			}
			if (list != null)
			{
				for (int j = 0; j < list.Count; j++)
				{
					list[j].PatientInfo.ID = StartMark + (StartIndex + Interval * j).ToString(StringFormat);
				}
			}
			IsModify = true;
			if (this.OnAutoIndexChanged != null)
			{
				this.OnAutoIndexChanged(sender, new EventArgs());
			}
		}

		private void buttonCancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}
	}
}
